package com.atguigu.spring.iocxml.process;

import com.atguigu.spring.iocxml.pojo.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanProcessor implements BeanPostProcessor {

    private final Logger logger = LoggerFactory.getLogger(MyBeanProcessor.class);

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        LifeCycle lifeCycle = (LifeCycle)bean;
        lifeCycle.setAge(lifeCycle.getAge() - 5);
        logger.info("初始化前 : " + beanName + " = " + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        LifeCycle lifeCycle = (LifeCycle)bean;
        lifeCycle.setAge(lifeCycle.getAge() + 10);
        logger.info("初始化后 : " + beanName + " = " + bean);
        return bean;
    }
}